using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

namespace DotConferenceSql
{
    /// <summary>
    /// CompositeQueryElement is the most important class in the SQL query generator.
    /// It collects all elements required to build a full SQL query.
    /// </summary>
    public class CompositeQueryElement : QueryElementBase
    {
        private List<QueryElementBase> m_elements = new List<QueryElementBase>();

        /// <summary>
        /// Gets the elements that made the query
        /// </summary>
        /// <value>The elements.</value>
        public List<QueryElementBase> Elements
        {
            get
            {
                return m_elements;
            }
        }

        /// <summary>
        /// Generate a SQL query
        /// </summary>
        /// <returns>generated SQL query</returns>
        public override string BuildTextQuery()
        {
            // Sort the elements to be in right order
            // to generate a valid query.
            Elements.Sort(QueryElementComparator);

            StringBuilder finalQuery = new StringBuilder();

            // Don't try to generate a query without elements
            if (Elements.Count == 0)
            {
                System.Diagnostics.Debug.WriteLine("CompositeQueryElement.Elements is empty");
                return String.Empty;
            }

            QueryElementType latestType = QueryElementType.Other;
            foreach (QueryElementBase queryElement in Elements)
            {
                if (latestType != queryElement.Type)
                {
                    latestType = queryElement.Type;
                    // Add space between the different clauses
                    finalQuery.Append(" ");
                    switch (latestType)
                    {
                        case QueryElementType.SelectClause:
                            finalQuery.Append("SELECT ");
                            break;
                        case QueryElementType.InsertIntoClause:
                            finalQuery.Append("INSERT INTO ");
                            break;
                        case QueryElementType.DeleteClause:
                            finalQuery.Append("DELETE FROM ");
                            break;
                        case QueryElementType.UpdateClause:
                            finalQuery.Append("UPDATE ");
                            break;
                        case QueryElementType.FromClause:
                            finalQuery.Append("FROM ");
                            break;
                            // InnerJoinQueryElement will generate its on INNER JOIN to allow multiple
                            // INNER JOIN statements
                        case QueryElementType.InnerJoinClause:
                            break;
                        case QueryElementType.OrderByClause:
                            finalQuery.Append("ORDER BY ");
                            break;
                        case QueryElementType.WhereClause:
                            finalQuery.Append("WHERE ");
                            break;
                        case QueryElementType.GroupByClause:
                            finalQuery.Append("GROUP BY ");
                            break;
                    }
                }

                finalQuery.Append(queryElement.BuildTextQuery());
            }

            return finalQuery.ToString().Trim();
        }

        /// <summary>
        /// Aggregate the parameters from the elements
        /// and generate the complete list of parameters
        /// for BuildSqlCommand()
        /// </summary>
        /// <returns></returns>
        public override List<SqlParameter> ListParameters()
        {
            List<SqlParameter> newList = new List<SqlParameter>();
            newList.AddRange(Parameters);

            foreach (QueryElementBase element in Elements)
            {
                newList.AddRange(element.ListParameters());
            }

            return newList;
        }

       
    }
}